home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C
/
Applications
/
Python 1.3.3
/
Python 133 68K
/
Lib
/
test
/
img
/
NOTYET
/
imgpict.py
< prev
Wrap
Text File
|
1996-05-20
|
3KB
|
103 lines
#
# Macintosh PICT format reader/writer module
#
# Jack Jansen, CWI, 1995
#
import struct
import imgformat
error = 'imgpict.error'
class reader:
def __init__(self, filename):
self._filename = filename
self.width = 0
self.height = 0
raise error, 'Reading PICT files not yet supported'
def args(self):
return self.__dict__
def read(self):
return ''
def write(self, data):
raise error, 'Cannot write() to reader'
class writer:
def __init__(self, filename):
self._filename = filename
self.format_choices = (imgformat.rgb, )
self.format = imgformat.rgb
def args(self):
return self.__dict__
def _get(self, attr):
try:
return getattr(self, attr)
except AttributeError:
raise error, "Required attribute '%s' missing"%attr
def read(self):
raise error, 'Cannot read() from writer'
def write(self, data):
w = self._get('width')
h = self._get('height')
fmt = self._get('format')
if fmt <> imgformat.rgb:
raise error, 'Only rgb currently supported'
if w*h*4 != len(data):
raise error, 'Incorrect datasize'
# Create the image structure. This is tricky due to possible alignment
imgstruct = struct.pack('hhhhh', 0x011, 0x02ff, 0x0c00, -2, 0)[:10]
imgstruct = imgstruct + struct.pack('llhhhh', 0x480000, 0x480000, 0, 0, h, w)
imgstruct = imgstruct + struct.pack('l', 0)
imgstruct = imgstruct + struct.pack('h', 0x9a)[:2]
imgstruct = imgstruct + struct.pack('l', 0xff)
imgstruct = imgstruct + struct.pack('hhhhhhh', 0x8000|(w*4),
0, 0, h, w, 0, 1)[:14]
imgstruct = imgstruct + struct.pack('lll', 0, 0x480000, 0x480000)
imgstruct = imgstruct + struct.pack('hhhh', 16, 32, 3, 8)
## imgstruct = imgstruct + struct.pack('lll', 0, 0, 0)
## imgstruct = imgstruct + struct.pack('l', 0x1000000)
## imgstruct = imgstruct + struct.pack('hh', 0, 0)
## imgstruct = imgstruct + struct.pack('hhhh', -1, 0, 0, 0)
imgstruct = imgstruct + struct.pack('lll', 0, 0xad892c, 0) # ????
imgstruct = imgstruct + struct.pack('hhhh', 0, 0, h, w)
imgstruct = imgstruct + struct.pack('hhhh', 0, 0, h, w)
imgstruct = imgstruct + struct.pack('h', 0)[:2]
length = len(imgstruct) + len(data) + 10 + 2
imghdr = struct.pack('hhhhh', length&0xffff, 0, 0, h, w)[:10]
imgtrailer = struct.pack('h', 0xff)[:2]
fp = open(self._filename, 'w')
fp.write('\0'*512)
fp.write(imghdr)
fp.write(imgstruct)
fp.write(data)
fp.write(imgtrailer)
fp.close()
try:
import macfs
macfs.FSSpec(self._filename).SetCreatorType('????', 'PICT')
except ImportError:
# We're probably not running on a mac
pass
def _test():
import imgppm
r = imgppm.reader('in-rgb-t2b.ppm')
w = writer('out-mac.pict')
w.width, w.height = r.width, r.height
d = r.read()
w.write(d)
if __name__ == '__main__':
_test()